package com.edu.suda.housing.match;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.util.*;

import test.EditDistanceSimilarityFunction;
import tfidf.CountIFIDF;
import database.DBconnection;
import house.House;
public class BucketsInMatch {
	
	public DBconnection db = DBconnection.getDbConnection();
	public LinkedList <House> houseList = new LinkedList<House>();
	public LinkedList <House> innerHouse = new LinkedList<House>();
	public HashMap<Integer,TempHouseInfo> houseTemp = new HashMap<Integer,TempHouseInfo>();
	public Map<String, LinkedList<House>> buckets = new HashMap<String, LinkedList<House>>();
	public HashMap<String, LinkedList<House>> urlbuckets = new HashMap<String, LinkedList<House>>();
	
	EditDistanceSimilarityFunction sim = new EditDistanceSimilarityFunction();	
 
	CountIFIDF cii = new CountIFIDF();
	//此处控制操作的表，每次替换以 bianma_pre_ganji_**开头的表
   //String[] tableName={"pre_ganji_bj","pre_ganji_cd","pre_ganji_gz","pre_ganji_hz","pre_ganji_nj","pre_ganji_sh","pre_ganji_su","pre_ganji_sz","pre_ganji_tj","pre_ganji_wh"};	
	String tableName= "bianma_pre_ganji_bj";
	public  String sql = "select * from "+tableName;
	
	//每次以表名作为输出文件，如
	//String[] tableName={"pre_ganji_bj","pre_ganji_cd","pre_ganji_gz","pre_ganji_hz","pre_ganji_nj","pre_ganji_sh","pre_ganji_su","pre_ganji_sz","pre_ganji_tj","pre_ganji_wh"};
	File file=new File("file/bianma_pre_ganji_bj.txt");
	
	//将所得数据转为House对象
	public LinkedList<House> dataTwoHouse(String sql){
		try{
			ResultSet rs = db.executeQuery(sql);
			while(rs.next()){
				House house = new House(rs);
				houseList.add(house);
			}
			rs.close();
		}catch(Exception e){
			System.out.println("database read exception");
		}
		return houseList;
	}
	//根据“编码”字段得到大桶
	public Map<String, LinkedList<House>> getBuckets() throws UnsupportedEncodingException, IOException{
		try{
			ResultSet rs = db.executeQuery(sql);
			while(rs.next()){
				if(!buckets.containsKey(rs.getString("编码"))){
					buckets.put(rs.getString("编码"), new LinkedList<House>());
					buckets.get(rs.getString("编码")).add(new House(rs));
				}else{
					buckets.get(rs.getString("编码")).add(new House(rs));
				}
			}
			rs.close();
		}catch(Exception e){
			System.out.println("database read exception"+e.toString());
		}
		//输出大桶的信息
		for(String key:buckets.keySet()){
			 LinkedList<House> house = new LinkedList<House>();
			 house = buckets.get(key);
			 if(!file.exists()||file.isDirectory()) throw new FileNotFoundException();
	   		    FileOutputStream out = new FileOutputStream(file,true);
			    for(int index =0; index<house.size();index++){
				   	StringBuffer sb = new StringBuffer();
		  			sb.append("房源编号:  "+house.get(index).id+"   编码:"+house.get(index).code+"  URL:"+house.get(index).url+"\n");
		  			out.write(sb.toString().getBytes("utf-8"));
				 //System.out.println("房源编号:  "+house.get(index).id+"   编码:"+house.get(index).code+"  URL:"+house.get(index).url);	 
			 }
			 
				//System.out.println("=============大桶分割线=============");
			    out.write("\n=============大桶分割线=============\n".toString().getBytes("utf-8"));
		}
		return buckets;
	}
	
	//根据URL将大桶进一步细分
	public HashMap<String, LinkedList<House>> getSmallBuckets() throws UnsupportedEncodingException, IOException{	
	  for(String code: buckets.keySet()){
				for(House h : buckets.get(code)){
					if(!urlbuckets.containsKey(h.url)){
						urlbuckets.put(h.url, new LinkedList<House>());
						urlbuckets.get(h.url).add(h);
					}else{
						urlbuckets.get(h.url).add(h);
					}
				}
			}
	  //输出url桶的信息
	  if(!file.exists()||file.isDirectory()) throw new FileNotFoundException();
		    FileOutputStream out = new FileOutputStream(file,true);
			for(String key:urlbuckets.keySet()){
				 LinkedList<House> house = new LinkedList<House>();
				 house = urlbuckets.get(key);
				 for(int index =0; index<house.size();index++){
					 StringBuffer sb = new StringBuffer();
			  			sb.append("房源编号:  "+house.get(index).id+"   编码:"+house.get(index).code+"  URL:"+house.get(index).url+"\n");
			  			out.write(sb.toString().getBytes("utf-8"));
					 //System.out.println("房源编号:  "+house.get(index).id+"   编码:"+house.get(index).code+"  URL:"+house.get(index).url);	 
				 }
				 out.write("\n=============url桶分割线=============\n".toString().getBytes("utf-8"));
				 
					//System.out.println("=============url桶分割线=============");
			}
		return urlbuckets;
	}	
	
	//存储房源临时信息的三元组
	class TempHouseInfo{
		public int id;
		public String bucketsId;
		public LinkedList<House> house;
		public LinkedList<Integer> houseId;
		
			public int getId() {
				return id;
			}
			public void setId(int id) {
				this.id = id;
			}
			public String getBucketsId() {
				return bucketsId;
			}
			public void setBucketsId(String bucketsId) {
				this.bucketsId = bucketsId;
			}
			public LinkedList<House> getHouse() {
				return house;
			}
			public void setHouse(LinkedList<House> house) {
				this.house = house;
			}
			public LinkedList<Integer> getHouseId() {
				return houseId;
			}
			public void setHouseId(LinkedList<Integer> houseId) {
				this.houseId = houseId;
			}	 
	}
	//融合大桶的信息和url桶的信息
	public HashMap<Integer,TempHouseInfo> getTempInfo() throws UnsupportedEncodingException, IOException{
		int id = 0;
		for(String code : buckets.keySet()){
			for(String u : urlbuckets.keySet()){
				TempHouseInfo temp = new TempHouseInfo();
				LinkedList<House> houseUrl = new LinkedList<House>();
				LinkedList<Integer> houseId = new LinkedList<Integer>();
				for(House h1 : buckets.get(code)){
					for(House h2 : urlbuckets.get(u)){
						if(h1.id == h2.id){
							houseUrl.add(h1);
							houseId.add(h1.id);
							break;
						}
					}
				}
				temp.setId(id);
				temp.setBucketsId(code);
				temp.setHouse(houseUrl);
				temp.setHouseId(houseId);
				if(houseId.size() != 0){
				houseTemp.put(id++, temp);
				}
			}
		}
		
		 if(!file.exists()||file.isDirectory()) throw new FileNotFoundException();
		    FileOutputStream out = new FileOutputStream(file,true);
		    
		for(int i:houseTemp.keySet()){
			TempHouseInfo temp =houseTemp.get(i);
			 StringBuffer sb = new StringBuffer();
	  			sb.append("小桶的id："+temp.getHouseId()+"  小桶编码: "+temp.getBucketsId()+"  小桶URL: "+temp.getHouse().get(0).url+"\n");
	  			out.write(sb.toString().getBytes("utf-8"));
			//System.out.println("小桶的id："+temp.getHouseId()+"  小桶编码: "+temp.getBucketsId()+"  小桶URL: "+temp.getHouse().get(0).url);
		}
		 out.write("\n=============输出结束=============\n".toString().getBytes("utf-8"));
		return houseTemp;
	}
	public static void main(String[]args) throws UnsupportedEncodingException, IOException{
		BucketsInMatch bim = new BucketsInMatch();
		bim.getBuckets();
		//System.out.println("\n~~~~~~~~~~~~~~~~~~大桶、url输出分割线~~~~~~~~~~~~~~~~~~~~");
		bim.getSmallBuckets();
		//System.out.println("\n~~~~~~~~~~~~~~~~~~输出分割线~~~~~~~~~~~~~~~~~~~~");
		//bim.getTempInfo();
	}
}